package com.ruoyi.common.core.utils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.utils.sql.SqlUtil;
import com.ruoyi.common.core.web.domain.BaseEntity;
import com.ruoyi.common.core.web.page.TableDataInfo;

import java.util.List;

/**
 * 分页工具类
 * 
 * @author ruoyi
 */
public class PageUtils extends PageHelper
{
    /**
     * 设置请求分页数据
     */
    public static void startPage(BaseEntity dto)
    {
        PageHelper
                .startPage(
                        Convert.toInt(dto.getPageNum(), 1),
                        Convert.toInt(dto.getPageSize(), 10),
                        SqlUtil.escapeOrderBySql(dto.getOrderBy()))
                .setReasonable(dto.getReasonable());
    }

    /**
     * 响应请求分页数据
     */
    public static <T extends BaseEntity> TableDataInfo<T> toTableData(List<? extends BaseEntity> list, List<T> voList) {
        PageInfo<BaseEntity> pageInfo = new PageInfo<>(list);
        TableDataInfo<T> labTableData = new TableDataInfo<>();
        labTableData.setCode(HttpStatus.SUCCESS);
        labTableData.setMsg("查询成功");
        labTableData.setTotal(pageInfo.getTotal());
        labTableData.setRows(voList);
        return labTableData;
    }

    /**
     * 响应请求分页数据
     */
    public static <T> TableDataInfo<T> toTableData(List<T> list) {
        TableDataInfo<T> labTableData = new TableDataInfo<>();
        labTableData.setCode(HttpStatus.SUCCESS);
        labTableData.setMsg("查询成功");
        labTableData.setRows(list);
        return labTableData;
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage()
    {
        PageHelper.clearPage();
    }
}
